import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决导航栏或者底部导航tabBar中的vue-router在3.0版本以上频繁点击菜单报错的问题。
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'main',
    component: () => import( '../views/Main.vue'),
    redirect: '/home',  //重定向到主页
    children:[
      {path: 'home' ,name: 'Home', meta:{name:'系统管理'}, component: () =>import( '../views/Main/Home.vue')},
      {path: '403' ,name: 'Auth', meta:{name:'无权限'}, component: () =>import( '../views/Main/Auth.vue')},
      {path: '/userinfo', name: 'Userinfo', meta:{name:'员工信息'}, component: ()=>import('../views/Main/Userinfo.vue')},
      {path: '/person', name: 'Person', meta:{name:'个人信息'}, component: ()=>import('../views/Main/Person.vue')},
      {path: '/password', name: 'Password', meta:{name:'修改密码'}, component: ()=>import('../views/Main/Password.vue')},
      {path: '/problem', name: 'Problem', meta:{name:'设备异常详情'}, component: ()=>import('../views/Main/Problem.vue')},
      {path: '/problemDetail', name: 'ProblemDetail', meta:{name:'问题详情页面'}, component: ()=>import('../views/Main/ProblemDetail.vue')},
      {path: '/notice', name: 'Notice', meta:{name:'系统公告'}, component: ()=>import('../views/Main/Notice.vue')},
      {path: '/logs', name: 'Logs', meta:{name:'系统日志'}, component: ()=>import('../views/Main/Logs.vue')},
      {path: '/assign', name: 'Assign', meta:{name:'数据统计分析'}, component: ()=>import('../views/Main/Assign.vue')},
      {path: '/realtime', name: 'Realtime', meta:{name:'灌溉实时数据'}, component: ()=>import('../views/Main/Realtime.vue')},
      {path: '/standard', name: 'Standard', meta:{name:'灌溉标准数据'}, component: ()=>import('../views/Main/Standard.vue')},
      {path: '/farmland', name: 'Farmland', meta:{name:'农田管理'}, component: ()=>import('../views/Main/Farmland.vue')},
    ]
  },

  {path: '/login', name: 'Login',meta:{name:'登录'},  component: ()=>import('../views/Login.vue')},
  {path: '/register', name: 'Register',meta:{name:'注册'},  component: ()=>import('../views/Register.vue')},
  {path: '*', name: '404',meta:{name:'无法访问'},  component: ()=>import('../views/404.vue')},

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  //  to 是到达的路由信息
  //from 是开源的路由信息
  //next 是帮助我们跳转路由的函数
  let adminPath =['/123']
  let user = JSON.parse(localStorage.getItem('honey-usr') || '{}')

  if (user.role !== '管理员' && adminPath.includes(to.path)){     //如果当前登陆的用户不是管理员，然后当前到达的路径是管理员才有权限访问的路径，那这个时候我就让用户去到一个没有权限的页面，不让他访问实际的页面
    next('/403')
  } else {
    next()
  }
})

export default router
